package StudentAchievementManagementSystem.component;

import StudentAchievementManagementSystem.mysql.MysqlUtil;
import StudentAchievementManagementSystem.util.ScreenUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateStuGradeDialog extends JDialog {
    final int WIDTH =350;
    final int HEIGHT=200;

    public UpdateStuGradeDialog(JFrame jf, String title, Boolean isModel) {
        super(jf, title, isModel);
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装学号
        Box snoBox = Box.createHorizontalBox();
        JLabel snoLabel = new JLabel("学号");
        JTextField snoTextField = new JTextField(15);

        snoBox.add(snoLabel);
        snoBox.add(Box.createHorizontalStrut(20));
        snoBox.add(snoTextField);

        //组装课程号
        Box cnoBox = Box.createHorizontalBox();
        JLabel cnoLabel = new JLabel("课程号");
        JTextField cnoTextField = new JTextField(15);

        cnoBox.add(cnoLabel);
        cnoBox.add(Box.createHorizontalStrut(20));
        cnoBox.add(cnoTextField);

        //组装成绩
        Box gradeBox = Box.createHorizontalBox();
        JLabel gradeLabel = new JLabel("成绩");
        JTextField gradeTextField = new JTextField(15);

        gradeBox.add(gradeLabel);
        gradeBox.add(Box.createHorizontalStrut(20));
        gradeBox.add(gradeTextField);

        //组装添加按钮
        Box btnBox = Box.createHorizontalBox();
        JButton button = new JButton("更新");
        btnBox.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sno = snoTextField.getText().trim();
                String cno = cnoTextField.getText().trim();
                String grade=gradeTextField.getText().trim();

//                String sql="select ";
                Connection connection = null;
                try {
                    connection = MysqlUtil.getConnection();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
                Statement statement = null;
                try {
                    statement = connection.createStatement();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                //判断表中是否有sno，cno数据
                String Sqltest="select sno from studentcoursegrade where sno='"+sno+"' and cno='"+cno+"' ";
                try {
                    ResultSet resultSet1 = statement.executeQuery(Sqltest);
                    if (resultSet1.next()){
                        //更新的sql语句
                        String sql="update studentcoursegrade set grade ='"+grade+"' where sno ='"+sno+"'and cno='"+cno+"'" ;
                        try {
                            statement.execute(sql);
                            //读取grade判断是否更新成功
                            String Sqltest2="select grade from studentcoursegrade where sno='"+sno+"' and cno='"+cno+"' ";
                            ResultSet resultSet2 = statement.executeQuery(Sqltest2);
                            while(resultSet2.next()) {
                                if (resultSet2.getString(1).equals(grade)) {
                                    JOptionPane.showMessageDialog(jf, "更新成功");
                                    dispose();
                                } else {
                                    JOptionPane.showMessageDialog(jf, "更新失败");
                                }
                            }
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }else{JOptionPane.showMessageDialog(jf,"没有相匹配的sno，cno数据！");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });


        vBox.add(Box.createVerticalStrut(20));
        vBox.add(snoBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(cnoBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(gradeBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnBox);

        this.add(vBox);
    }
}
